/**
 * 模拟 Android 系统上 Toast 行为. 需要 jquery 库.
 *
 * $.Toast.init (options);
 * $.Toast.show (message);
 *
 * 常量：
 *     名称                               值
 *     $.Toast.Layout.CENTER              0   指示在屏幕中央显示消息
 *     $.Toast.Layout.NORTH               1   在屏幕上方显示消息
 *     $.Toast.Layout.SOUTH               2   在屏幕下方显示消息
 *     $.Toast.Layout.WEST                4   在屏幕左侧显示消息
 *     $.Toast.Layout.EAST                8   在屏幕右侧显示消息
 *
 * 选项：
 *     direction                        int    以上常量的组合，比如，在屏幕右上角显示消息时，可传入值 9
 *                                             即 $.Toast.Layout.NORTH | $.Toast.Layout.EAST，
 *                                             默认为 $.Toast.Layout.SOUTH
 *     interval                         int    淡入淡出的时长，单位毫秒，默认为 1000
 *     duration                         int    消息在屏幕上停留的时长，单位为毫秒，默认为 3000
 *
 *
 * Created with IntelliJ IDEA.
 * User: seth.yang
 * Date: 15-1-22
 * Time: 上午2:17
 * To change this template use File | Settings | File Templates.
 */
(function ($) {
    if ($.Toast) return;
    var root = $('<div/>').addClass ('toast-base').css ({opacity:.5}), W, H, done = false, inited = false;
    $(function () {
        root.appendTo ($(document.body));
        var win = $(window);
        W = win.width ();
        H = win.height();
        done = true;
    });
    $.Toast = (function ($) {
        return {
            init : function (options) {
                if (!inited) {
                    this.settings = $.extend ({}, this.settings, options || {});
                    var NORTH = this.Layout.NORTH, SOURTH = this.Layout.SOURTH,
                        EAST = this.Layout.EAST, WEST = this.Layout.WEST,
                        props = {}, direction = this.settings ['direction'];

                    if ((NORTH & direction) != 0) {
                        props.top = 20;
                    } else if ((SOURTH & direction) != 0) {
                        props.bottom = 20;
                    }
                    if ((EAST & direction) != 0) {
                        props.right = 20;
                    } else if ((WEST & direction) != 0) {
                        props.left = 20;
                    }
                    root.css (props);
                    inited = true;
                }
                return this;
            },
            show : function (text, delay) {
                if (!inited) this.init ();
                if (done) {
                    root.text (text);
                    var W = $(window).width (), H = $(window).height (),
                        w = root.outerWidth (), h = root.outerHeight ();
                    var interval = this.settings ['interval'] || 1000,
                        duration = this.settings ['duration'] || 3000,
                        direction = this.settings ['direction'];
                    if (direction == this.Layout.CENTER)
                        root.css ({left:Math.floor ((W - w) / 2), top: Math.floor ((H - h) / 2)});
                    else if (direction == this.Layout.NORTH ||
                             direction == this.Layout.SOURTH) {
                        root.css ({left:Math.floor ((W - w) / 2)});
                    }
                    root.fadeIn (interval, function () {
                        var handler = setTimeout(function () {
                            root.fadeOut (interval);
                            clearTimeout (handler);
                        }, delay || duration);
                    });
                }
            },
            reset : function () {
                root.removeAttr ('style');
            }
        };
    }) ($);
    $.Toast.Layout = {CENTER: 0, NORTH : 1, EAST : 2, SOURTH: 4, WEST: 8};
    $.Toast.settings = {interval : 1000, duration : 3000, direction: $.Toast.Layout.SOURTH};
}) (jQuery);